package com.ruoyi.web.controller.module.attendance;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.attendance.AttendanceGroup;
import com.ruoyi.system.domain.module.attendance.AttendanceOffWork;
import com.ruoyi.system.service.module.attendance.IAttendanceGroupService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.module.attendance.IAttendanceOffWorkService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 考勤的 调休假期Controller
 *
 * @author ruoyi
 * @date 2022-10-08
 */
@Validated
@Api(value = "考勤的 调休假期控制器", tags = {"考勤的 调休假期管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/attendance/work")
public class AttendanceOffWorkController extends BaseController {

    private final IAttendanceOffWorkService attendanceOffWorkService;
    private final IAttendanceGroupService attendanceGroupService;

    /**
     * 查询考勤的 调休假期列表
     */
    @ApiOperation("查询考勤的 调休假期列表")
    @PreAuthorize("@ss.hasPermi('module:attendanceWork:list')")
    @GetMapping("/list")
    public TableDataInfo<AttendanceOffWork> list(@Validated AttendanceOffWork attendanceOffWork) {
        QueryWrapper<AttendanceOffWork> qw = new QueryWrapper<>();
        if(attendanceOffWork.getGroupId()!=null){
            qw.eq("group_id",attendanceOffWork.getGroupId());
        }
        TableDataInfo<AttendanceOffWork> td = PageUtils.buildDataInfo(attendanceOffWorkService.page(PageUtils.buildPage(),qw));
        List<AttendanceOffWork> list = td.getRows();
        for (AttendanceOffWork list1:list){
            if(list1.getGroupId()!=null&&list1.getGroupId()!=0){
                AttendanceGroup ag=attendanceGroupService.findById(list1.getGroupId());
                if (ag!=null){
                    list1.setGroupName(ag.getGroupName());
                }
            }
        }
        return td;
    }

    /**
     * 导出考勤的 调休假期列表
     */
    @ApiOperation("导出考勤的 调休假期列表")
    @PreAuthorize("@ss.hasPermi('module:attendanceWork:export')")
    @Log(title = "考勤的 调休假期", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<AttendanceOffWork> export(@Validated AttendanceOffWork attendanceOffWork) {
        List<AttendanceOffWork> list = attendanceOffWorkService.list();
        ExcelUtil<AttendanceOffWork> util = new ExcelUtil<AttendanceOffWork>(AttendanceOffWork.class);
        return util.exportExcel(list, "考勤的 调休假期");
    }

    /**
     * 获取考勤的 调休假期详细信息
     */
    @ApiOperation("获取考勤的 调休假期详细信息")
    @PreAuthorize("@ss.hasPermi('module:attendanceWork:query')")
    @GetMapping("/{id}")
    public AjaxResult<AttendanceOffWork> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        AttendanceOffWork attendanceOffWork = attendanceOffWorkService.getById(id);
        return AjaxResult.success(attendanceOffWork);
    }

    /**
     * 新增考勤的 调休假期
     */
    @ApiOperation("新增考勤的 调休假期")
    @PreAuthorize("@ss.hasPermi('module:attendanceWork:add')")
    @Log(title = "考勤的 调休假期", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody AttendanceOffWork attendanceOffWork) {
        attendanceOffWork.setIsDefault(0);
        return toAjax(attendanceOffWorkService.save(attendanceOffWork) ? 1 : 0);
    }

    /**
     * 修改考勤的 调休假期
     */
    @ApiOperation("修改考勤的 调休假期")
    @PreAuthorize("@ss.hasPermi('module:attendanceWork:edit')")
    @Log(title = "考勤的 调休假期", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody AttendanceOffWork attendanceOffWork) {
        return toAjax(attendanceOffWorkService.updateById(attendanceOffWork) ? 1 : 0);
    }

    /**
     * 删除考勤的 调休假期
     */
    @ApiOperation("删除考勤的 调休假期")
    @PreAuthorize("@ss.hasPermi('module:attendanceWork:remove')")
    @Log(title = "考勤的 调休假期" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(attendanceOffWorkService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
